
*------------------------------------------------------------------------------*
* Basics: 1st and 2nd chamber median, position of president
*
* Version:	2016-06-10
* Changes:	2016-??-??: First release
*------------------------------------------------------------------------------*


* Save temporary data set
save zs, replace


* 1st chamber median 
gen filterseat1st = p303 if $ideology != .
collapse (median) zs1 = $ideology [fweight = filterseat1st], by(g101 g105)
gen median1st = float(zs1)
drop zs1
sort g101 g105
save zs_median, replace
use zs, clear	// merge
merge g101 g105 using zs_median, uniqusing sort
drop _merge
sort g101 g105
save zs, replace


* 2nd chamber median 
summarize p402	// check existence of 2nd chamber...
if r(N) != 0 {
	gen filterseat2nd = p402 if $ideology != .
	collapse (median) zs2 = $ideology [fweight = filterseat2nd], by(g101 g105)
	gen median2nd = float(zs2)
	drop zs2
	sort g101 g105
	save zs_median, replace
	use zs, clear	// merge
	merge g101 g105 using zs_median, uniqusing
	drop _merge
	sort g101 g105
	save zs, replace
}
else {			// ...else generate empty variable
	gen median2nd = .
}


* Position of president
summarize p502	// check existence of president...
if r(N) != 0 {
	gen zspres = $ideology if p502 == 1
	by g101 g105, sort: egen pres = mean(zspres)
}
else {			// ...else generate empty variable
	gen pres = .
}


* Cleansing
replace median1st = . if g106 == 9
replace median2nd = . if g106 == 9
replace pres 	  = . if g106 == 9

lab var median1st "Position of 1st chamber median parlamentarian"
lab var median2nd "Position of 2nd chamber median parlamentarian"
lab var pres 	  "Position of president"

sort g101 g105 p101

capture: drop zspres
capture: erase zs.dta
capture: erase zs_median.dta
